﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using TMDT_Nhom1A.BUS;
using TMDT_Nhom1A.DTO;
using System.Collections.Generic;
using System.Drawing;

namespace TMDT_Nhom1A.UserControl_Admin
{
    public partial class AccountBankWebserviceManager : System.Web.UI.UserControl
    {
        private static List<AccountBankWebserviceDTO> accBankWsInfo = new List<AccountBankWebserviceDTO>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string id = Request.QueryString["id"];
                //id = "3";
                if (id != null)
                {
                    accBankWsInfo = AccountBankWebserviceBUS.getAccountBankWS(id);
                    if (accBankWsInfo.Count >= 1)
                    {
                        btAdd.Visible = false;
                        gvAccountBankWSList.DataSource = accBankWsInfo;
                        gvAccountBankWSList.DataBind();
                    }
                    else
                    {
                        btAdd.Visible = true;
                    }
                }

            }
        }

        protected void btAdd_Click(object sender, ImageClickEventArgs e)
        {
            //tao dữ liệu giả de bind len datalist
            List<AccountBankWebserviceDTO> temp = new List<AccountBankWebserviceDTO>();
            AccountBankWebserviceDTO account = new AccountBankWebserviceDTO();
            account.Id = 5;

            temp.Add(account);
            DlAddUpdateAccountBankWS.EditItemIndex = -1;
            DlAddUpdateAccountBankWS.DataSource = temp;
            DlAddUpdateAccountBankWS.DataBind();
            DlAddUpdateAccountBankWS.Visible = true;

        }

        protected void gvAccountBankWSList_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void gvAccountBankWSList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {

                //tim thong tin trong list
                List<AccountBankWebserviceDTO> temp = new List<AccountBankWebserviceDTO>();
                for (int i = 0; i < accBankWsInfo.Count; i++)
                {
                    if (accBankWsInfo[i].Id == int.Parse(e.CommandArgument.ToString()))
                    {
                        temp.Add(accBankWsInfo[i]);
                    }
                }

                DlAddUpdateAccountBankWS.EditItemIndex = 0;
                DlAddUpdateAccountBankWS.Visible = true;
                DlAddUpdateAccountBankWS.DataSource = temp;
                DlAddUpdateAccountBankWS.DataBind();


            }

            if (e.CommandName == "btDelete")
            {
                int id = int.Parse(e.CommandArgument.ToString());
                bool result = AccountBankWebserviceBUS.deleteAccountBankWS(id);
                if (result == true)
                {
                    for (int i = 0; i < accBankWsInfo.Count; i++)
                    {
                        if (accBankWsInfo[i].Id == id)
                        {
                            accBankWsInfo.RemoveAt(i);
                        }
                    }
                    //refesh lai gridview va hien thong bao
                    gvAccountBankWSList.DataSource = accBankWsInfo;
                    gvAccountBankWSList.DataBind();
                }
                else
                {
                    LbThongBao.Text = "Xoá Thất Bại";
                    LbThongBao.ForeColor = Color.Red;
                }
            }
        }

        protected void DlAddUpdateAccountBankWS_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "Add")
            {
                TextBox tbUsername = (TextBox)e.Item.FindControl("tbUsername");
                TextBox tbPassword = (TextBox)e.Item.FindControl("tbPassword");
                TextBox tbMaTaiKhoan = (TextBox)e.Item.FindControl("tbMaTaiKhoan");

                string id = Request.QueryString["id"];

                if (id != null)
                {
                    AccountBankWebserviceDTO account = new AccountBankWebserviceDTO();
                    account.Username = tbUsername.Text;
                    account.Password = tbPassword.Text;
                    account.MaTaiKhoan = tbMaTaiKhoan.Text;
                    account.BankId = id.ToString();
                    bool result = AccountBankWebserviceBUS.insertAccountBankWS(account);
                    if (result == true)
                    {
                        accBankWsInfo.Add(account);
                        btAdd.Visible = false;
                        //refesh lai gridview va hien thong bao
                        accBankWsInfo = AccountBankWebserviceBUS.getAccountBankWS(id);
                        gvAccountBankWSList.DataSource = accBankWsInfo;
                        gvAccountBankWSList.DataBind();
                        DlAddUpdateAccountBankWS.Visible = false;

                    }
                    else
                    {
                        LbThongBao.Text = "Thêm Thất Bại";
                        LbThongBao.ForeColor = Color.Red;
                    }
                }
                else
                {
                    LbThongBao.Text = "Thêm Thất Bại";
                    LbThongBao.ForeColor = Color.Red;
                }




            }

            if (e.CommandName == "Update")
            {

                Label tbId = (Label)e.Item.FindControl("id");
                TextBox tbUsername = (TextBox)e.Item.FindControl("tbUsername");
                TextBox tbPassword = (TextBox)e.Item.FindControl("tbPassword");
                TextBox tbMaTaiKhoan = (TextBox)e.Item.FindControl("tbMaTaiKhoan");

                string bankId = Request.QueryString["id"];
                if (bankId != null)
                {

                    AccountBankWebserviceDTO account = new AccountBankWebserviceDTO();
                    account.Id = int.Parse(tbId.Text);
                    account.Username = tbUsername.Text;
                    account.Password = tbPassword.Text;
                    account.MaTaiKhoan = tbMaTaiKhoan.Text;
                    account.BankId = bankId;
                    bool result = AccountBankWebserviceBUS.updateAccountBankWS(account);
                    if (result == true)
                    {
                        for (int i = 0; i < accBankWsInfo.Count; i++)
                        {
                            if (accBankWsInfo[i].Id == int.Parse(tbId.Text))
                            {
                                accBankWsInfo[i] = account;
                            }
                        }

                        //refesh lai gridview va hien thong bao
                        gvAccountBankWSList.DataSource = AccountBankWebserviceBUS.getAccountBankWS(bankId);
                        gvAccountBankWSList.DataBind();
                        DlAddUpdateAccountBankWS.Visible = false;

                    }
                    else
                    {
                        LbThongBao.Text = "Cập Nhật Thất Bại";
                        LbThongBao.ForeColor = Color.Red;
                    }
                }
                else
                {
                    LbThongBao.Text = "Cập Nhật Thất Bại";
                    LbThongBao.ForeColor = Color.Red;
                }

            }

            if (e.CommandName == "Cancel")
            {
                DlAddUpdateAccountBankWS.Visible = false;
            }
        }

        protected void gvAccountBankWSList_RowEditing(object sender, GridViewEditEventArgs e)
        {

        }

        
    }
}